package com.atguigu.mybatisplus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.mybatisplus.mapper.DeptMapper;
import com.atguigu.mybatisplus.model.entity.Dept;
import com.atguigu.mybatisplus.model.form.DeptForm;
import com.atguigu.mybatisplus.model.vo.DeptVO;
import com.atguigu.mybatisplus.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【sys_dept(部门表)】的数据库操作Service实现
 * @createDate 2025-08-23 09:54:23
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    /**
     * 部门列表
     */
    @Override
    public List<DeptVO> getDeptList(String keywords, Long status) {
        // 构造查询条件
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Dept::getName, keywords)
                .eq(Dept::getStatus, status);
        // 查询部门列表
        List<Dept> depts = deptMapper.selectList(queryWrapper);
        // 封装部门VO列表
        List<DeptVO> deptVOList = new ArrayList<>();
        for (Dept dept : depts) {
            DeptVO deptVO = new DeptVO();
            BeanUtil.copyProperties(dept, deptVO);
            deptVOList.add(deptVO);
        }
        return deptVOList;
    }

    /**
     * 更新部门
     * @param deptId 部门ID
     * @param deptForm 部门表单
     */
    @Override
    public void updateDept(Long deptId, DeptForm deptForm) {
        Dept dept = deptMapper.selectById(deptId);
        BeanUtil.copyProperties(deptForm, dept);
        deptMapper.updateById(dept);
    }

    /**
     * 新增部门
     * @param deptForm 部门表单
     */
    @Override
    public void saveDept(DeptForm deptForm) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptForm, dept);
        deptMapper.insert(dept);
    }

    /**
     * 获取部门表单
     * @param deptId 部门ID
     * @return 部门表单
     */
    @Override
    public DeptForm getFormById(Long deptId) {
        Dept dept = deptMapper.selectById(deptId);
        DeptForm deptForm = new DeptForm();
        BeanUtil.copyProperties(dept, deptForm);
        return deptForm;
    }
}




